/**
 * Models a Document that can be saved or stored.
 * 
 * @author Chris Baillie.
 */

package application;
import java.util.ArrayList;

public class Essay {
	//Instance Vars
	private String subject;
	private String author;
	public ArrayList<String> paragraphs = new ArrayList<String>();
	public ArrayList<String> sentences = new ArrayList<String>();
	public ArrayList<String> words = new ArrayList<String>();
	
	public Essay(String subject, String author)
	{
		this.subject = subject;
		this.author = author;
	}
	
	public void setContents(String contents)
	{
		splitToPara(contents);
	}
	
	public void setContentsFromDb(String contents)
	{
		splitToSent(contents);
		splitToWords(contents);
	}
	
	public void splitToPara(String contents)
	{
		String[] paras = contents.split("[\n\t]|[\n\n]|[\n\r]|[\n][ ]+");
		for(int i=0;i<paras.length;i++){
			if(!paras[i].equals("")){
				paragraphs.add(paras[i]);
			}
		}
	}
	
	public void splitToSent(String contents)
	{
		String[] sents = contents.split("[.]{1} +");
		for(int i=0;i<sents.length;i++){
			sentences.add(sents[i]);
		}
	}
	
	public void splitToWords(String contents)
	{
		String[] wds = contents.split(" ");
		for(int i=0;i<wds.length;i++)
			words.add(wds[i]);
	}
	
	public void getHeader()
	{
		System.out.println(subject + " by " + author);
	}
	
	public ArrayList<String> getParagraphs()
	{
		return paragraphs;
	}
	
	public String getSentence(int sentNum)
	{
		String sentence = "";
		try{
			sentence = (String)sentences.get(sentNum--);
		} catch(Exception e){
			System.err.println("Data Access Error: ");
			e.printStackTrace();
		}
		return sentence;
	}
	
	public String getWord(int wordNum)
	{
		String word = "";
		try{
			word = (String)words.get(wordNum--);
		}catch(Exception e){
			System.err.println("Data Access Error: ");
			e.printStackTrace();
		}
		return word;
	}

	public String getContents()
	{	
		System.out.println("Let's go...!");
		String content = null;
		for(int i=0;i<sentences.size();i++){
			content += " " + sentences.get(i);
		}
		System.out.println("I'm done...");
		return content;
	}
	
	public String getSubject()
	{
		return subject;
	}
	
	public String getAuthor()
	{
		return author;
	}
	
}
